﻿using FolderTag.Core;
using FolderTag.DB;
using FolderTag.DB.Dbo;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace FolderTag.Model
{
    public class PathItem
    {
        public PathItem()
        {
            Type = EPathType.None;
            PathName = string.Empty;
            PathExtension = string.Empty;
            TagList = new List<TagItem>();
        }

        public PathItem(FolderPathDbo dbo) : this()
        {
            Id = dbo.Id.Value;
            Desc = dbo.Desc;
            Path = dbo.Path;
            ParentPath = dbo.ParentPath;
            InitPath();
            if (Type == EPathType.None)
                FolderPathDb.Delete(Id);
            else
            {
                if (!string.IsNullOrEmpty(dbo.TagIds))
                {
                    var temp = dbo.TagIds.Split(",");
                    TagList = FolderTagDb.GetByIds(new HashSet<int>(temp.Select(x => Convert.ToInt32(x))));
                }
            }
        }

        public PathItem(DriveInfo drive) : this()
        {
            Id = -1;
            Desc = drive.VolumeLabel;
            Path = drive.Name;
            ParentPath = "";
            Type = EPathType.Disk;
        }

        public PathItem(FileSystemInfo fileSystem, List<PathItem> dbList) : this()
        {
            Id = 0;
            Desc = "";
            Path = fileSystem.Name;
            ParentPath = fileSystem.FullName.Replace(Path, "");
            InitPath();
            PathItem dbItem = dbList.Where(x => x.Path.Equals(fileSystem.Name)).FirstOrDefault();
            if (dbItem != null)
            {
                Id = dbItem.Id;
                Desc = dbItem.Desc;
                TagList = dbItem.TagList;
            }
        }

        public PathItem(NetResource netResource, string remoteName) : this()
        {
            Id = -1;
            Desc = "";
            Path = netResource.RemoteName.Replace(remoteName, "");
            ParentPath = remoteName;
            Type = EPathType.Disk;//在Net共享目录中，在根目录下的分享目录Type为Disk（net view \\xxx）
        }

        public string GetFullPath()
        {
            return System.IO.Path.Combine(ParentPath, Path);
        }

        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Desc { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
        public EPathType Type { get; set; }

        /// <summary>
        /// 真实路径
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// 路径名称
        /// </summary>
        public string PathName { get; set; }
        /// <summary>
        /// 路径后缀
        /// </summary>
        public string PathExtension { get; set; }
        /// <summary>
        /// 父路径
        /// </summary>
        public string ParentPath { get; set; }
        /// <summary>
        /// 标签列表
        /// </summary>
        public List<TagItem> TagList { get; set; }

        /// <summary>
        /// 初始化目录
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private void InitPath()
        {
            string fullPath = GetFullPath();
            if (Directory.Exists(fullPath))
            {
                Type = EPathType.Directory;
                PathName = Path;
            }
            else if (File.Exists(fullPath))
            {
                Type = EPathType.File;
                FileInfo fileInfo = new FileInfo(fullPath);
                PathName = System.IO.Path.GetFileNameWithoutExtension(fullPath);
                PathExtension = fileInfo.Extension;
                Regex regex = new Regex("(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$");
                if (regex.IsMatch(PathExtension))
                    Type = EPathType.Image;
            }
        }
    }
}
